package com.fowo.api.ptt.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.ptt.enitty.PrintTemplate;
import com.fowo.api.ptt.model.PrintTemplateSearchParamPo;
import com.fowo.api.ptt.service.PrintTemplateService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 打印模版控制器
 * @author yl_ls
 */
@Tag(name = "PrintTemplate", description = "打印模版")
@RestController
@RequestMapping("api/sys/print-template")
public class PrintTemplateController {
    @Resource
    private PrintTemplateService printTemplateService;

    @Operation(summary = "获取用户可见模版组")
    @GetMapping("groups")
    public R<?> getGroups() {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            return R.fail("此接口不支持无身份方式访问");
        }
        return R.data(() -> printTemplateService.getVisibleTemplateGroups(jwtUserInfo.getUserId()));
    }

    @Operation(summary = "获取用户可见模版组下属模板类型")
    @GetMapping("groups/{groupId}/types")
    public R<?> getTypesInGroup(@PathVariable Long groupId) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            return R.fail("此接口不支持无身份方式访问");
        }
        return R.data(() -> printTemplateService.getVisibleTypesByGroupId(groupId, jwtUserInfo.getUserId()));
    }

    @Operation(summary = "获取指定模板类型信息")
    @GetMapping("types/{id}")
    public R<?> getTypeById(@PathVariable Long id) {
        return R.data(() -> printTemplateService.getTypeById(id));
    }

    @Operation(summary = "设置模板类型的默认模板")
    @PutMapping("types/{typeId}/default-template-id/{id}")
    public R<?> setTypeDefaultTemplateId(@PathVariable(name = "typeId") Long typeId, @PathVariable(name = "id") Long id) {
        return R.run(() -> printTemplateService.setTypeDefaultTemplateId(typeId, id));
    }

    @Operation(summary = "清空模板类型的默认模板")
    @PutMapping("types/{typeId}/default-template-id/null")
    public R<?> setTypeDefaultTemplateId(@PathVariable(name = "typeId") Long typeId) {
        return R.run(() -> printTemplateService.setTypeDefaultTemplateId(typeId, null));
    }

    @Operation(summary = "模板分页查询")
    @GetMapping("page")
    public R<?> pageSearch(PrintTemplateSearchParamPo searchParamPo) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
            searchParamPo.setCurrentUserId(jwtUserInfo.getUserId());
        }
        return R.data(printTemplateService.pageSearch(searchParamPo));
    }

    @Operation(summary = "获取指定模版")
    @GetMapping("{id}")
    public R<?> getById(@PathVariable Long id) {
        return R.data(() -> printTemplateService.getById(id));
    }

    @Operation(summary = "创建模板")
    @PostMapping
    public R<?> create(@RequestBody PrintTemplate printTemplate) {
        return R.data(() -> printTemplateService.create(printTemplate));
    }

    @Operation(summary = "更新模板")
    @PutMapping
    public R<?> update(@RequestBody PrintTemplate printTemplate) {
        return R.run(() -> printTemplateService.update(printTemplate));
    }

    @Operation(summary = "删除模板")
    @DeleteMapping("{id}")
    public R<?> delete(@PathVariable Long id) {
        return R.run(() -> printTemplateService.delete(id));
    }
}
